package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.dto.SocketMsgPageDto;
import com.hoongs.v1.entity.SocketMsg;
import com.hoongs.v1.entity.SocketMsgRecord;
import com.hoongs.v1.mapper.SocketMsgMapper;
import com.hoongs.v1.mapper.SocketMsgRecordMapper;
import com.hoongs.v1.service.SocketMsgService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SocketMsgServiceImpl implements SocketMsgService {

    public static final String NOT_FOUND_MSG = "没有找到该通知！";

    @Resource
    private SocketMsgMapper socketMsgMapper;
    @Resource
    private SocketMsgRecordMapper socketMsgRecordMapper;

    /**
     * 设置已读
     * @param id
     * @return
     */
    @Override
    public Result read(String id) {
        SocketMsgRecord socketMsg = socketMsgRecordMapper.selectById(id);
        if (socketMsg == null) return Result.opError("未找到该消息！");
        socketMsg.setIsRead("1");
        if (socketMsgRecordMapper.updateById(socketMsg) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }


    @Override
    public Result add(SocketMsg socketMsg) {
        if (socketMsgMapper.insert(socketMsg) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
//        if (!findArticleById(id)) return Result.opError(NOT_FOUND_MSG);
        if (socketMsgMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(SocketMsg socketMsg) {
        return null;
    }

    @Override
    public Result selectById(String id) {
        SocketMsg socketMsg = socketMsgMapper.selectById(id);
        if (socketMsg == null) return Result.error(NOT_FOUND_MSG);
        return Result.success(socketMsg);
    }

    @Override
    public Result selectPageList(SocketMsgPageDto socketMsgDto) {
        if (!PResult.checkPageQuery(socketMsgDto.getPage(), socketMsgDto.getSize())) return Result.error("分页参数错误");
        Page<SocketMsg> daoPage = new Page<>(socketMsgDto.getPage(), socketMsgDto.getSize());
        QueryWrapper<SocketMsg> wrapper = new QueryWrapper<SocketMsg>();
        if (StringUtils.isNotBlank(socketMsgDto.getType())) {
            wrapper.eq("type", socketMsgDto.getType());
        }
        if (StringUtils.isNotBlank(socketMsgDto.getToId())) {
            wrapper.eq("to_id", socketMsgDto.getToId());
        }
        if (StringUtils.isNotBlank(socketMsgDto.getFromId())) {
            wrapper.eq("from_id", socketMsgDto.getFromId());
        }
        IPage<SocketMsg> socketMsgIpage = socketMsgMapper.selectPage(daoPage, wrapper);
        PResult rs = new PResult(socketMsgIpage);
        return Result.success(rs);
    }
}
